package com.study.controller;
import com.study.common.ResultJson;
import com.study.pojo.Orders;
import com.study.pojo.Thumb;
import com.study.service.OrdersService;
import com.study.service.ThumbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 处理点赞，和加入书架等等，请求。
 * @Author Miss kun
 * @Date 2020/10/16 17:05
 * @Version 1.0
 */
@RequestMapping("/like")
@Controller
public class LikeController {

    @Autowired
    private ThumbService thumbService;

    @Autowired
    private OrdersService ordersService;


    /**
     * 添加用户点赞信息，同时该书的总点赞数+1
     * @return
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public ResultJson changeThumbStatus(@RequestParam("userId")Integer userId, @RequestParam("bookId") Integer bookId, @RequestParam("status") Integer status){
        //先判断该用户是否点赞了该书
        //没有的话存储该用户点赞本书的信息
        //存在的话，直接修改本书的状态信息
        if(!thumbService.findThumbByUserIdAndBookId(userId, bookId)){
            thumbService.insertThumb(new Thumb(userId,bookId,status));
        }else {
            thumbService.updateStatus(userId,bookId,status);
        }
        return ResultJson.ok();
    }

    /**
     * 判断该用户是否点赞了这本书
     * @param userId
     * @param bookId
     * @return
     */
    @RequestMapping("/show")
    @ResponseBody
    public boolean showThumbIcon(@RequestParam("userId")Integer userId, @RequestParam("bookId") Integer bookId){
        Thumb thumbByUserIdAndBook = thumbService.findThumbByUserIdAndBook(userId, bookId);
        if (thumbByUserIdAndBook != null && thumbByUserIdAndBook.getThumb_status()== 1){
            return true;
        }
        return false;
    }

    /**
     * 修改用户订阅状态
     * @param userId
     * @param bookId
     * @param status
     * @return
     */
    @RequestMapping("/changeOrder")
    @ResponseBody
    public String changeOrder(@RequestParam("userId")Integer userId, @RequestParam("bookId") Integer bookId, @RequestParam("chapter_title") String chapter_title, @RequestParam("chapter_id")Integer chapter_id, @RequestParam("status") Integer status){
        //先判断该用户是否订阅了本书
        //没有的话存储该用户点赞本书的信息
        //存在的话，直接修改本书的状态信息
        if(!ordersService.findOrdersByUserIdAndBookId(userId, bookId)){
            ordersService.insertOrders(new Orders(userId,bookId,chapter_id,chapter_title,status));
        }else {
            ordersService.updateStatus(userId,bookId,status);
        }
        return "true";
    }
    /**
     * 判断该用户是否订阅了这本书
     * @param userId
     * @param bookId
     * @return
     */
    @RequestMapping("/see")
    @ResponseBody
    public boolean showOrdersIcon(@RequestParam("userId")Integer userId, @RequestParam("bookId") Integer bookId){
        Orders orders = ordersService.findOrdersByUserIdAndBook(userId, bookId);
        if (orders != null && orders.getOrders_status()== 1){
            return true;
        }
        return false;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public List<Orders> deleteOrders(@RequestParam("id") Integer id, @RequestParam("userId")Integer userId){
        ordersService.updateBookStatus(id,0);
        List<Orders> books = ordersService.getMyOrdersBook(userId, 1);
        return books;
    }
}
